/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved.
 *
 */

import { ProjectBuildOption, TestCaseFun } from '../../plugins/task-builder/test-case';
import {
  AbilityType,
  APIType,
  APIVersion, CaseLevel,
  LanguageType, ModuleTemplateType,
  RuntimeOSType,
  SDKType
} from '../../plugins/project-builder/models/constants';
import { getJson5Obj } from '../../../../src/utils/json-util';
import path from 'path';
import {
  addDependency,
  addHarAbility,
  initExecuteCommand,
  runHvigorAsync, setDefaultStrictMode
} from '../../common/common';
import { expect } from '@jest/globals';
import { TestCaseBuilder } from '../../plugins/task-builder/task-case-builder';
import { TestTaskOptionBuilder } from '../../plugins/task-builder/test-task-option';
import { ModuleJson } from '../../../../src/options/configure/module-json-options';
import ModuleOptObj = ModuleJson.ModuleOptObj;

import AbilityObj = ModuleJson.AbilityObj;

const projectBuilderOption: ProjectBuildOption = {
  template: {
    sdk: SDKType.Single,
    runtimeOS: RuntimeOSType.HarmonyOS,
    apiVersion: APIVersion.API11,
    apiType: APIType.STAGE,
    language: LanguageType.ArkTs,
    abilityType: AbilityType.Application,
    entryTemplateType: ModuleTemplateType.EmptyAbility
  },
  modulesOption: {
    [ModuleTemplateType.StaticLibrary]: 1,
    [ModuleTemplateType.SharedLibrary]: 1
  }
};

const testfun: TestCaseFun = async (project, taskConfig, record) => {
    const projectRootPath = project.projectRootPath();
    setDefaultStrictMode(projectRootPath);

    // har添加ability
    const harRootPath = project.getModule('staticLibrary').moduleRootPath();
    await addHarAbility(harRootPath, projectRootPath);

    // hsp添加本地源码har依赖
    const hspRootPath = project.getModule('shareLibrary').moduleRootPath();
    await addDependency(hspRootPath, { 'staticLibrary': 'file:../staticLibrary' });

    const executeCommand = initExecuteCommand({
      sync: true,
      mode: 'module',
      module: 'shareLibrary',
      tasks: 'assembleHsp'
    });
    await runHvigorAsync(project.projectRootPath(), executeCommand);

    // 查看包产物module.json:build/default/intermediates/package/default/module.json
    const hspIntermediatesPkgModuleJsonPath = path.resolve(projectRootPath, 'shareLibrary/build/default/intermediates/package/default/module.json');
    const hspIntermediatesPkgModuleJsonObj: ModuleOptObj = getJson5Obj(hspIntermediatesPkgModuleJsonPath);
    hspIntermediatesPkgModuleJsonObj.module.abilities?.forEach((abilityObj: AbilityObj) => {
      if (abilityObj.name === 'harAbility') {
        // 1.hsp产物module.json中包含har的ability
        expect(abilityObj.srcEntry === '@normalized:N&&&staticLibrary/src/main/ets/components/mainpage/MainPage&1.0.0').toBe(true);

        // 2.har的ability配置包含资源id
        expect(abilityObj.hasOwnProperty('iconId')).toBe(true)
        expect(abilityObj.hasOwnProperty('labelId')).toBe(true)
        expect(abilityObj.hasOwnProperty('startWindowIconId')).toBe(true)
        expect(abilityObj.hasOwnProperty('startWindowBackgroundId')).toBe(true)
      }
    });
  }
;

TestCaseBuilder.builder()
  .caseName('DTS2024103129409&DTS2024102913999')
  .description('hsp模块没有创建Ability，依赖har模块，hsp模块选择har模块的Ability，运行会报错；' +
    'hap/hsp收集依赖har的ability并合并到hap/hsp产物包中时相关资源id也需写入')
  .projectBuilderOption(projectBuilderOption)
  .taskConfig(TestTaskOptionBuilder.builder().caseLevel(CaseLevel.AR).build())
  .testFun(testfun)
  .load();
